﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace BAL
{
    public partial class AbanINStockDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;

        private string FiBllNo;
        private string FInterID;
        private string PkgsBarCode;
        private string PlaceBarCode;
        private string PlaceName;
        private string PkgsIsRegular;
        private string FEntryID;   
        private string Fry;
        private string FSku;
        private string StickLen;
        private string FQty;
        private string Fdate; 
        private string Floor;  
        private string Fsku;
        private string Stick;
        private string ID;
       
        
        public AbanINStockDetail()
        {
            InitializeComponent();

        }
        
        public AbanINStockDetail(string fno, string eid,string fry,string fSku,string stickLen,string fQty,string fdate)
        {
            InitializeComponent();
            FiBllNo = fno;
            FEntryID = eid;
            Fry= fry  ;
            FSku= fSku  ;
            StickLen= stickLen  ;
            FQty= fQty  ;
            Fdate= fdate  ;
            this.tb_PkgsBarCode.Focus();
        }


        public AbanINStockDetail(string fno, string eid, string fry, string fSku, string stickLen, string fQty, string fdate,string _ID)
        {
            InitializeComponent();
            FiBllNo = fno;
            FEntryID = eid;
            Fry = fry;
            FSku = fSku;
            StickLen = stickLen;
            FQty = fQty;
            Fdate = fdate;
            this.ID = _ID;
            this.tb_PkgsBarCode.Focus();
        }

        private void AbanINStockDetail_Load(object sender, EventArgs e)
        {
            if (FiBllNo == null || FiBllNo == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
                this.tb_PkgsBarCode.Focus();
            }
            tb_FBillNo.Text = FiBllNo.ToString();
        }

           private void BinData()
           {
               DataSet ds = bjs.GetAbanINStockDetail(FiBllNo, FEntryID);
               DataTable dt = new DataTable();
               dt = ds.Tables[0];
               //if(dt.Rows.Count<=0)
               //{
               //     return;
               //}
               //else
               //{
                    dataGrid1.DataSource=dt;
               //}
           }

        /// <summary>
        /// 数据窗口样式
        /// </summary>
          private void  DataGridStyle()
          {
              DataGridTableStyle dts = new DataGridTableStyle();
              dataGrid1.TableStyles.Clear();
              dts.MappingName="AbanINStockDetail";

              DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
              cl1.HeaderText = "流水号";
              cl1.MappingName="FInterID";
              cl1.Width = 40;
              dts.GridColumnStyles.Add(cl1);

              DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
              cl2.HeaderText="包装码";
              cl2.MappingName="PkgsBarCode";
              cl2.Width=80;
              dts.GridColumnStyles.Add(cl2);

              DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
              cl3.HeaderText="仓位码";
              cl3.MappingName="PlaceBarCode";
              cl3.Width=80;
              dts.GridColumnStyles.Add(cl3);

              DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
              cl4.HeaderText="仓位名";
              cl4.MappingName="PlaceName";
              cl4.Width=120;
              dts.GridColumnStyles.Add(cl4);

              DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
              cl5.HeaderText="类别";
              cl5.MappingName="PkgsIsRegular";
              cl5.Width=40;
              dts.GridColumnStyles.Add(cl5);

              DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
              cl6.HeaderText = "Fsku";
              cl6.MappingName = "FSku";
              cl6.Width = 150;
              dts.GridColumnStyles.Add(cl6);

              DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
              cl7.HeaderText = "物料名";
              cl7.MappingName = "FItemName";
              cl7.Width = 110;
              dts.GridColumnStyles.Add(cl7);

              DataGridTextBoxColumn cl8 = new DataGridTextBoxColumn();
              cl8.HeaderText = "属性名";
              cl8.MappingName = "AttrValue";
              cl8.Width = 110;
              dts.GridColumnStyles.Add(cl8);             

              DataGridTextBoxColumn cl9 = new DataGridTextBoxColumn();
              cl9.HeaderText = "根长";
              cl9.MappingName = "StickLen";
              cl9.Width = 30;
              dts.GridColumnStyles.Add(cl9);

              DataGridTextBoxColumn cl10 = new DataGridTextBoxColumn();
              cl10.HeaderText = "数量";
              cl10.MappingName = "FQty";
              cl10.Width = 30;
              dts.GridColumnStyles.Add(cl10);

              dataGrid1.TableStyles.Add(dts);

          }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int  index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count >0)
            {
              ((DataGrid)sender).Select(index);
            }
            FInterID =((DataTable)(dataGrid1.DataSource)).Rows[index]["FInterID"].ToString();
            PkgsBarCode =((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsBarCode"].ToString();
            PlaceBarCode =((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceBarCode"].ToString();
            PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceName"].ToString();
            PkgsIsRegular =((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsIsRegular"].ToString();
        
            this.tb_FBillNo.Text=FiBllNo.Trim();
            this.tb_FInterID.Text=FInterID.Trim();
            this.tb_PkgsBarCode.Text=PkgsBarCode.Trim();
            this.tb_PlaceBarCode.Text = PlaceBarCode.Trim();
            this.tb_PlaceName.Text = PlaceName.Trim();         
          
            this.comboBox1.Text = PkgsIsRegular;

            this.tb_PkgsBarCode.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AbanINStock ais = new AbanINStock(this.ID);
            ais.Show();
            this.Hide();
        }


        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == null || PkgsBarCode == "")
                {
                    MessageBox.Show("请扫描包装包装码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    bool result = bjs.GetAbanINCode(PkgsBarCode, FiBllNo, out  PlaceBarCode, out  PlaceName, out FInterID, out PkgsIsRegular);

                    //bool result = bjs.GetCode(PkgsBarCode, out  PlaceBarCode, out  PlaceName, out Floor, out  Fsku, out  StickLen, out  Stick, out  FQty, out  PkgsIsRegular);
                    if (result)
                    {
                        this.tb_FInterID.Text=FInterID;
                        this.comboBox1.Text = PkgsIsRegular;
                        this.tb_PlaceName.Text = PlaceName;
                        this.tb_PkgsBarCode.Text =PkgsBarCode;
                        this.tb_PlaceBarCode.Text = PlaceBarCode.Trim();
                        this.button1.Focus();
                      
                    }
                    else
                    {
                        MessageBox.Show("没有该包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

                       
                        this.comboBox1.Text = "";
                        this.tb_PlaceName.Text = "";
                        this.tb_PkgsBarCode.Text = "";
                        this.tb_PlaceBarCode.Text = "";
                        return;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FInterID = this.tb_FInterID.Text.Trim();
            PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();
            PlaceBarCode = this.tb_PlaceBarCode.Text.Trim();
            PkgsIsRegular = this.comboBox1.Text.Trim();

            if (FInterID == null || FInterID == "")
            {
                MessageBox.Show("请选择入库记录！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            if (PkgsBarCode == null || PlaceBarCode == "")
            {
                MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            if (PlaceBarCode == null || PlaceBarCode == "")
            {
                MessageBox.Show("请扫描仓位码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            bool result = bjs.UpdateAbanINStock(FInterID, PkgsBarCode, PlaceBarCode, PkgsIsRegular, Program.userid);
            if (result == true)
            {
                DataGridStyle();
                BinData();
                this.tb_PkgsBarCode.Focus();
                this.comboBox1.Text = null;
                this.tb_PkgsBarCode.Text = "";
                this.tb_PlaceBarCode.Text = "";
                this.tb_PlaceName.Text = "";
                this.tb_PkgsBarCode.Focus();

            }
            else
            {
                MessageBox.Show("提交失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
        }
       
      

        private void tb_PlaceBarCode_KeyDown(object sender ,KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                PlaceBarCode = this.tb_PlaceBarCode.Text;
                string FNumber = "";//仓位代码
                bool lb_return = bjs.GetPlaceName(PlaceBarCode, out FNumber, out PlaceName);

                if(lb_return)
                {
                    this.tb_PlaceName.Text=PlaceName;
                    this.button1.Focus();
                }
                else
                {
                    MessageBox.Show("仓位码不存在！", "警告！",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button3);
                    return;
                }
            }
        
        }

    }
    }
